package br.com.abenildo.visao;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Color;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JSeparator;

import br.com.abenildo.controle.Deficiente;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.InputMismatchException;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class JFrameCadastrarNoSistema extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldCpf;
	private JTextField textFieldNome;
	JFrameMenu telaMenu = new JFrameMenu();
	Deficiente objdeficiente = new Deficiente();
	private JTextField textFieldContato;
	private JTextField textFieldBairro;
	private JTextField textFieldRua;
	private JTextField textFieldNumero;
	JFrameLoguin telaloguin = new JFrameLoguin();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrameCadastrarNoSistema frame = new JFrameCadastrarNoSistema();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public JFrameCadastrarNoSistema() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.out.println("Sistema Disponivel!");
			}
		});
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("GRUPO FACABEMFEITO");
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(new Color(230, 230, 250));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblCpf = new JLabel("CPF");
		lblCpf.setBounds(45, 29, 46, 14);
		contentPane.add(lblCpf);
		
		textFieldCpf = new JTextField();
		textFieldCpf.setBounds(101, 26, 86, 20);
		contentPane.add(textFieldCpf);
		textFieldCpf.setColumns(10);
		
		JLabel lblNome = new JLabel("Nome");
		lblNome.setBounds(45, 64, 46, 14);
		contentPane.add(lblNome);
		
		textFieldNome = new JTextField();
		textFieldNome.setBounds(101, 64, 86, 20);
		contentPane.add(textFieldNome);
		textFieldNome.setColumns(10);
		
		JLabel lblTipoDefi = new JLabel("Tipo Defi");
		lblTipoDefi.setBounds(36, 100, 55, 14);
		contentPane.add(lblTipoDefi);
		
		final JComboBox comboBoxTipoDefi = new JComboBox();
		comboBoxTipoDefi.setModel(new DefaultComboBoxModel(new String[] {" Selecione", "  Visual", "  Normal"}));
		comboBoxTipoDefi.setBounds(101, 97, 84, 20);
		contentPane.add(comboBoxTipoDefi);
		
		JLabel lblSexo = new JLabel("Sexo");
		lblSexo.setBounds(36, 134, 46, 14);
		contentPane.add(lblSexo);
		
		final JComboBox comboBoxSexo = new JComboBox();
		comboBoxSexo.setModel(new DefaultComboBoxModel(new String[] {"\tSelecione", "\tMasculino", "\tFeminino"}));
		comboBoxSexo.setBounds(101, 128, 86, 20);
		contentPane.add(comboBoxSexo);
		
		JButton btnConfirmar = new JButton("Confirmar");
		btnConfirmar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try{
					objdeficiente.setCpf(textFieldCpf.getText());
					objdeficiente.setNome(textFieldNome.getText());
					objdeficiente.setBairro(textFieldBairro.getText());
					objdeficiente.setContato(textFieldContato.getText());
					objdeficiente.setNumero(textFieldNumero.getText());
					objdeficiente.setRua(textFieldRua.getText());
					objdeficiente.setSexo(comboBoxSexo.getSelectedItem().toString());
					objdeficiente.setTipo_Defic(comboBoxTipoDefi.getSelectedItem().toString());
					
				}catch(InputMismatchException e){
					JOptionPane.showMessageDialog(null, "Entrada de validos invalida!");
					
				}catch(Exception e){
					JOptionPane.showMessageDialog(null, e.getMessage());
				}
				//Serve para inpedir que abra a mesma tela
				if(telaMenu != null){
					JOptionPane.showMessageDialog(null, "Cadastrado com sucesso!");
					telaMenu.setVisible(true);
					
				}
				 fecharTelaAtual();
				
			}
		});
		btnConfirmar.setBackground(new Color(216, 191, 216));
		btnConfirmar.setBounds(101, 186, 96, 23);
		contentPane.add(btnConfirmar);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				limparCampos();
			}
		});
		btnCancelar.setBackground(new Color(216, 191, 216));
		btnCancelar.setBounds(225, 187, 89, 23);
		contentPane.add(btnCancelar);
		
		JSeparator separator = new JSeparator();
		separator.setBackground(new Color(205, 92, 92));
		separator.setBounds(36, 159, 366, 2);
		contentPane.add(separator);
		
		JLabel lblContato = new JLabel("Contato");
		lblContato.setBounds(212, 29, 46, 14);
		contentPane.add(lblContato);
		
		textFieldContato = new JTextField();
		textFieldContato.setBounds(268, 26, 118, 20);
		contentPane.add(textFieldContato);
		textFieldContato.setColumns(10);
		
		JLabel lblBairro = new JLabel("Bairro");
		lblBairro.setBounds(212, 67, 46, 14);
		contentPane.add(lblBairro);
		
		textFieldBairro = new JTextField();
		textFieldBairro.setBounds(250, 64, 136, 20);
		contentPane.add(textFieldBairro);
		textFieldBairro.setColumns(10);
		
		JLabel lblRua = new JLabel("Rua");
		lblRua.setBounds(212, 103, 46, 14);
		contentPane.add(lblRua);
		
		textFieldRua = new JTextField();
		textFieldRua.setBounds(250, 97, 138, 20);
		contentPane.add(textFieldRua);
		textFieldRua.setColumns(10);
		
		JLabel lblNumero = new JLabel("Numero");
		lblNumero.setBounds(212, 134, 46, 14);
		contentPane.add(lblNumero);
		
		textFieldNumero = new JTextField();
		textFieldNumero.setBounds(260, 131, 126, 20);
		contentPane.add(textFieldNumero);
		textFieldNumero.setColumns(10);
	}
	
	 public void fecharTelaAtual(){	 
		 this.dispose();
	 }
	 
	 public void limparCampos(){
		 textFieldCpf.setText("");
		 textFieldNome.setText("");
		 textFieldContato.setText("");
		 textFieldBairro.setText("");
		 textFieldRua.setText("");
		 textFieldNumero.setText("");
		 
	 }
	  
}
